package org.streets.workflow.engine.mem;

import java.util.ArrayList;
import java.util.List;

import org.streets.workflow.engine.ISyncRouter;
import org.streets.workflow.engine.event.INodeEventListener;
import org.streets.workflow.engine.plugin.INodeNetExtension;
import org.streets.workflow.model.net.WFStartNode;
import org.streets.workflow.model.net.WFSyncRouter;

/**
 * 
 */
public class StartNode extends AbstractNode implements ISyncRouter {

    public static final String Extension_Target_Name = "org.streets.workflow.mem.StartNode";
    public static List<String> Extension_Point_Names = new ArrayList<String>();
    public static final String Extension_Point_NodeInstanceEventListener = "NodeEventListener";


    static {
        Extension_Point_Names.add(Extension_Point_NodeInstanceEventListener);
    }
    
    private int volume = 0; //节点的容量

    private WFStartNode startNode = null;

    public StartNode(WFStartNode model) {
        this.type = NodeType.Start;
        this.startNode = model;
        volume = startNode.getLeavingTransitions().size();//  start 节点容量 ==输出弧的数量
    }

    public String getNodeId() {
        return this.startNode.getId();
    }

    @SuppressWarnings("unchecked")
    public WFSyncRouter getNodeModel() {
        return this.startNode;
    }

    public int getVolume() {
        return volume;
    }

    public void setVolume(int volume) {
        this.volume = volume;
    }

    public String getExtensionTargetName() {
        return Extension_Target_Name;
    }

    public List<String> getExtensionPointNames() {
        return Extension_Point_Names;
    }


    public void register(INodeNetExtension extension)
            throws RuntimeException {
    	
        if (!Extension_Target_Name.equals(extension.getExtentionTargetName())) {
            throw new RuntimeException(
                    "Error:When construct the StartNodeInstance,the Extension_Target_Name is mismatching");
        }
        if (Extension_Point_NodeInstanceEventListener.equals(extension.getExtentionPointName())) {
            if (extension instanceof INodeEventListener) {
                this.eventListeners.add((INodeEventListener) extension);
            } else {
                throw new RuntimeException(
                        "Error:When construct the StartNodeInstance,the extension MUST be a instance of INodeInstanceEventListener");
            }
        }
    }

    @Override
    public String toString() {
        return "StartNodeInstance_4_[" + startNode.getId() + "]";
    }

}
